package dao.Impl;

import dao.UserDao;
import model.UserModel;
import util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserImpl implements UserDao {
    @Override
    public int add(UserModel userModel) {
        String sql = " INSERT INTO `hs_user`(`username`, `password`)" +
                " VALUES ('" + userModel.getUsername() + "'," +
                "'" + userModel.getPassword() + "');\n";


        return JDBC.excuteUpdate(sql);
    }

    @Override
    public UserModel findByName(String username) {
        String sql = "select * from hs_user where username='" + username + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel userModel = null;
        try {
            while (resultSet.next()) {
                userModel = new UserModel();
//                userModel.setUsername(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userModel;
    }

    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {
        System.out.println(userModel.getUsername());
        String sql = "select * from hs_user where username='" + userModel.getUsername() + "' and password='" + userModel.getPassword() + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel model = null;
        try {
            while (resultSet.next()) {
                model = new UserModel();
//                userModel.setUsername(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }
}
